package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.string.smartwebsite.WebsiteStrings;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import com.demoo.zjsu.util.stringutil.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/18.
 * 图片处理器
 */
@Service
public abstract  class OnlyPhotoResolver extends IFactory{


    @Autowired
   SmartWebDAO dao;

    @Override
    public void resolver() {
        Page page = getPage();
        String category = page.getCategory();
        Document document = WebsiteUtil.getDocByURL(page.getUrl());
        Elements elements = getMain(document);
        dealPhotoAndFile(elements);
        List<PageTwo> pageTwos = new ArrayList<PageTwo>();
        getPageTwo(category, elements, pageTwos);
        dao.insertListPageTwo(pageTwos);
    }

    public void getPageTwo(String category, Elements elements, List<PageTwo> pageTwos) {
        for (Element element:elements) {
            PageTwo pageTwo = getPageTwo(element);
            if (pageTwo == null) {
                continue;
            }
            pageTwo.setCategory(category);
            pageTwos.add(pageTwo);
        }
    }

    /**
     * 其实之前所有的resolver设计成这样的粒度比较合适
     * 但是目前不打算更改了
     * 需要将一个集合抽出
     * @param element
     * @return
     */
    public abstract PageTwo getPageTwo(Element element);


    @Override
    public void filterElse(Elements elements) {

    }

    @Override
    public void addFile(Elements elements) {

    }

}
